package com.example.lovedogs.fragment;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;






import com.example.lovedogs.MainActivity;
import com.example.lovedogs.R;
import com.example.lovedogs.fragment.shop.ShangchengRightFragment;
import com.example.lovedogs.fragment.shop.shangchengLeftFragment;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

public class ShopFragment extends Fragment  {
	private ViewPager vp_attention;
	private View rootView;
	private ImageView[] imageViews;
	private boolean isContinue;
	private MainActivity activity;
	private AtomicInteger what = new AtomicInteger(0);
	private ShangchengRightFragment rigthFragment;
	private shangchengLeftFragment leftFragment;
 	private final class VpAdapter extends PagerAdapter {
		private List<View> views = null;

		public VpAdapter(List<View> views) {
			this.views = views;
		}

		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView(views.get(arg1));
		}

		@Override
		public void finishUpdate(View arg0) {
		}

		@Override
		public int getCount() {
			return views.size();
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(views.get(arg1), 0);
			return views.get(arg1);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}
	}

	private final class GuidePageChangeListener implements OnPageChangeListener {
		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		@Override
		public void onPageSelected(int arg0) {
			what.getAndSet(arg0);
			for (int i = 0; i < imageViews.length; i++) {
				imageViews[arg0]
						.setBackgroundResource(R.drawable.banner_dian_focus);
				if (arg0 != i) {
					imageViews[i]
							.setBackgroundResource(R.drawable.banner_dian_blur);
				}
			}
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_shop, container, false);
		activity=(MainActivity)getActivity();
		//横向滑动图片
		initViewPager();
		fragment();
		return rootView;
	}

	public void fragment() {
		FragmentManager fm = activity.getFragmentManager();
		leftFragment = (shangchengLeftFragment) fm.findFragmentById(R.id.fragment_left);
		rigthFragment = new ShangchengRightFragment();
		FragmentTransaction ft = fm.beginTransaction();
		activity.setRigthFragment(rigthFragment);
		ft.replace(R.id.fl_hold, rigthFragment);
		ft.addToBackStack(null);
		ft.commit();
	}
	private final Handler viewHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			vp_attention.setCurrentItem(msg.what);
			super.handleMessage(msg);
		}
	};

	private void initViewPager() {
		vp_attention = (ViewPager) rootView.findViewById(R.id.vp_attention);
		ViewGroup group = (ViewGroup) rootView.findViewById(R.id.viewGroup);
		// 这里存放的是四张广告背景
		List<View> advPics = new ArrayList<View>();
		ImageView img1 = new ImageView(getActivity());
		img1.setBackgroundResource(R.drawable.image01);
		advPics.add(img1);
		ImageView img2 = new ImageView(getActivity());
		img2.setBackgroundResource(R.drawable.image02);
		advPics.add(img2);
		ImageView img3 = new ImageView(getActivity());
		img3.setBackgroundResource(R.drawable.image03);
		advPics.add(img3);
		ImageView img4 = new ImageView(getActivity());
		img4.setBackgroundResource(R.drawable.image04);
		advPics.add(img4);

		// 对imageviews进行填充

		imageViews = new ImageView[advPics.size()];
		// 小图标
		ImageView imageView = null;
		for (int i = 0; i < advPics.size(); i++) {
			imageView = new ImageView(getActivity());
			imageView.setLayoutParams(new LayoutParams(20, 20));
			imageView.setPadding(5, 5, 5, 5);
			imageViews[i] = imageView;
			if (i == 0) {
				imageViews[i]
						.setBackgroundResource(R.drawable.banner_dian_focus);
			} else {
				imageViews[i]
						.setBackgroundResource(R.drawable.banner_dian_blur);
			}
			group.addView(imageViews[i]);
		}
		vp_attention.setAdapter(new VpAdapter(advPics));
		vp_attention.setOnPageChangeListener(new GuidePageChangeListener());
		vp_attention.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
				case MotionEvent.ACTION_MOVE:
					isContinue = false;
					break;
				case MotionEvent.ACTION_UP:
					isContinue = true;
					break;
				default:
					isContinue = true;
					break;
				}
				return false;
			}
		});
		new Thread(new Runnable() {
			@Override
			public void run() {
				while (true) {
					if (isContinue) {
						viewHandler.sendEmptyMessage(what.get());
						whatOption();
					}
				}
			}
		}).start();
	}

	private void whatOption() {
		what.incrementAndGet();
		if (what.get() > imageViews.length - 1) {
			what.getAndAdd(-4);
		}
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {

		}
	}
}
